﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormTest
{
    partial class UcListView
    {
        // 用于编辑的文本框
        private TextBox editTextBox;
        // 当前正在编辑的子项
        private ListViewItem.ListViewSubItem currentSubItem;
        private ListViewItem currentItem;

        private void InitializeEditTextBox()
        {
            editTextBox = new TextBox();
            editTextBox.Visible = false;
            editTextBox.KeyDown += EditTextBox_KeyDown;
            editTextBox.LostFocus += EditTextBox_LostFocus;
            Controls.Add(editTextBox);
        }


        private void OnMouseClick(object sender, MouseEventArgs e)
        {
            // 获取双击位置的子项
            ListViewHitTestInfo hitTest = HitTest(e.X, e.Y);
            // 检查是否命中了子项
            if (hitTest.SubItem != null)
            {
                // 获取子项所属的项
                ListViewItem item = hitTest.Item;

                // 获取子项在项中的索引（即列索引）
                int columnIndex = item.SubItems.IndexOf(hitTest.SubItem);

                // 获取列标题
                string columnHeaderText = Columns[columnIndex].Text;

                // 新文件名
                if (columnIndex==1)
                {
                    StartEditing(hitTest.SubItem,item);
                }
                else if (columnIndex == 4)
                {
                    this.Items.Remove(item);
                }
                else if (columnIndex == 5)
                {
                    string filePath = $"{item.SubItems[2].Text.TrimEnd('\\')}\\{item.SubItems[0].Text}";
                    // 使用资源管理器打开目录并选中文件
                    Process.Start(new ProcessStartInfo
                    {
                        FileName = "explorer.exe",
                        Arguments = $"/select, \"{filePath}\"",
                        UseShellExecute = true
                    });
                }
                // 示例：显示信息
                Console.WriteLine($"子项文本: {hitTest.SubItem.Text} 所在列索引: {columnIndex} 所在列标题: {columnHeaderText}");
            }
        }

        private void StartEditing(ListViewItem.ListViewSubItem subItem,ListViewItem item)
        {
            if (subItem == null) return;

            // 记录当前正在编辑的子项
            currentSubItem = subItem;
            currentItem = item;


            // 设置文本框位置和大小
            Rectangle rect = subItem.Bounds;
            rect.X = subItem.Bounds.X + 3;
            rect.Y = subItem.Bounds.Y + 3;
            rect.Width = subItem.Bounds.Width - 8;

            editTextBox.Bounds = rect;
            editTextBox.Text = subItem.Text;
            editTextBox.Visible = true;
            editTextBox.Focus();
            editTextBox.SelectAll();
        }
        private void EndEditing()
        {
            if (currentSubItem != null && editTextBox.Visible)
            {
                Console.WriteLine($"{currentSubItem.Text} {Path.GetExtension(currentSubItem.Text)}");
                // 更新子项文本
                if (currentSubItem.Text.Trim().Equals(editTextBox.Text.Trim()) == false)
                {
                    string ext = Path.GetExtension(currentItem.Text);
                    currentSubItem.Text = $"{editTextBox.Text.Trim()}{ext}";
                    //currentItem.SubItems[3].ForeColor = Color.Red;
                    //currentItem.SubItems[3].BackColor = Color.Black;
                    currentItem.SubItems[3].Text = "已修改";
                }
                editTextBox.Visible = false;
                currentSubItem = null;
            }
        }
        private void EditTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            // 按Enter键结束编辑
            if (e.KeyCode == Keys.Enter)
            {
                EndEditing();
                e.SuppressKeyPress = true; // 防止发出提示音
            }
            // 按Esc键取消编辑
            else if (e.KeyCode == Keys.Escape)
            {
                editTextBox.Visible = false;
                currentSubItem = null;
            }
        }

        private void EditTextBox_LostFocus(object sender, EventArgs e)
        {
            // 失去焦点时结束编辑
            EndEditing();
        }
    }
}
